package com.ruoyi.quartz.task;


import com.doudian.open.api.order_logisticsAdd.OrderLogisticsAddResponse;
import com.doudian.open.core.AccessToken;
import com.ruoyi.system.config.DouDianAPI;
import com.ruoyi.system.config.DouDianToken;
import com.ruoyi.system.domain.OrderAuthenticationTimer;
import com.ruoyi.system.domain.RxOrder;
import com.ruoyi.system.domain.RxOrderFollow;
import com.ruoyi.system.domain.RxOrderVo;
import com.ruoyi.system.mapper.OrderAuthenticationTimerMapper;
import com.ruoyi.system.mapper.RxOrderFollowMapper;
import com.ruoyi.system.mapper.RxOrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component("orderScan")
@Slf4j
public class OrderScan {
    @Autowired
    private RxOrderMapper rxOrderMapper;

    @Autowired
    private RxOrderFollowMapper rxOrderFollowMapper;

    /**
     * 订单跟进记录超48小时无跟进任务
     */
    public void chaoShi() {
        String osName = System.getProperty("os.name").toLowerCase();
        if (osName.contains("win")) {
            return;
        }

        List<RxOrderVo> rxOrderVos = rxOrderMapper.selectRxOrderChaoShiList();
        if (null != rxOrderVos && rxOrderVos.size() > 0) {
            for (RxOrderVo rv : rxOrderVos) {
                RxOrder rxOrder = new RxOrder();
                rxOrder.setId(Long.valueOf(rv.getId()));
                rxOrder.setState(9);
                rxOrder.setEmergentStatus(2L);
                rxOrder.setRovercount(rv.getRovercount() + 1);
                rxOrderMapper.updateRxOrder(rxOrder);
                RxOrderFollow rxOrderFollow = new RxOrderFollow();
                rxOrderFollow.setOrderId(Integer.valueOf(rv.getId()));
                rxOrderFollow.setTitle("【订单超时】");
                rxOrderFollow.setContent("订单处理已超时,请尽快联系客户跟进处理.");
                rxOrderFollow.setTime(System.currentTimeMillis() / 1000);
                rxOrderFollowMapper.insertRxOrderFollow(rxOrderFollow);
            }
        }
    }


    /**
     * 抖店下来的订单,在我们系统超47小时未审核,需要调抖店审核完成
     */
    public void audit47ChaoShi() {
        String osName = System.getProperty("os.name").toLowerCase();
        if (osName.contains("win")) {
            return;
        }
        List<RxOrder> rxOrderVos = rxOrderMapper.selectAuditChaoShi47List();
        if (null != rxOrderVos && rxOrderVos.size() > 0) {
            for (RxOrder rv : rxOrderVos) {
                AccessToken token = DouDianToken.getToken(rv.getDoudianOrderShopId());
                DouDianAPI.review("200", rv.getDoudianOrderId(), token);
                RxOrder newRxOrder = new RxOrder();
                newRxOrder.setId(Long.valueOf(rv.getId()));
                newRxOrder.setEmergentStatus(2L);
                rxOrderMapper.updateRxOrder(newRxOrder);
                RxOrderFollow rxOrderFollow = new RxOrderFollow();
                rxOrderFollow.setOrderId(rv.getId().intValue());
                rxOrderFollow.setTitle("【超时自动审核成功】");
                rxOrderFollow.setContent("订单超过47小时未操作审核,系统已自动审核成功,请知悉!");
                rxOrderFollow.setTime(System.currentTimeMillis() / 1000);
                rxOrderFollowMapper.insertRxOrderFollow(rxOrderFollow);
            }
        }
    }


    /**
     * 抖店下来的订单,在我们系统超24小时未审核,加急提醒操作员
     */
    public void audit24ChaoShi() {
        String osName = System.getProperty("os.name").toLowerCase();
        if (osName.contains("win")) {
            return;
        }
        List<RxOrder> rxOrderVos = rxOrderMapper.selectAuditChaoShi24List();
        if (null != rxOrderVos && rxOrderVos.size() > 0) {
            for (RxOrder rv : rxOrderVos) {

                if (2L != rv.getEmergentStatus()) {
                    RxOrder newRxOrder = new RxOrder();
                    newRxOrder.setId(Long.valueOf(rv.getId()));
                    newRxOrder.setEmergentStatus(2L);
                    rxOrderMapper.updateRxOrder(newRxOrder);
                }
                Long createTime = rv.getTime();
                Long currentTime = System.currentTimeMillis() / 1000;
                long hour = (currentTime - createTime) / 3600;
                long shenYuHour = (47 - hour);
                RxOrderFollow rxOrderFollow = new RxOrderFollow();
                rxOrderFollow.setOrderId(rv.getId().intValue());
                rxOrderFollow.setTitle("【审核提醒】");
                rxOrderFollow.setContent("订单已超过" + hour + "小时未操作审核,系统将在" + shenYuHour + "小时后自动审核成功,请及时操作审核!");
                rxOrderFollow.setTime(System.currentTimeMillis() / 1000);
                rxOrderFollowMapper.insertRxOrderFollow(rxOrderFollow);
            }
        }
    }

    @Autowired
    private OrderAuthenticationTimerMapper orderAuthenticationTimerMapper;

    /**
     * 扫描需要自动发货的订单
     * 抖店状态为10,且创建时间大于10分钟
     */
    public void authenticationTimer() throws InterruptedException {
        OrderAuthenticationTimer oAT = new OrderAuthenticationTimer();
        List<OrderAuthenticationTimer> oldOAT = orderAuthenticationTimerMapper.selectOrderAuthenticationTimerList(oAT);

        for (OrderAuthenticationTimer o : oldOAT) {
            RxOrder oldOrder = rxOrderMapper.selectRxOrderById(o.getOrderId());
            if (null != oldOrder && oldOrder.getDoudianOrderStatus() == 10) {
                RxOrderFollow rxOrderFollow = new RxOrderFollow();
                rxOrderFollow.setOrderId(oldOrder.getId().intValue());
                rxOrderFollow.setTime(System.currentTimeMillis() / 1000);
                Date currentDate = new Date();
                Long createDifferenceTime = (currentDate.getTime() - o.getCreateTime().getTime()) / 1000;
                if (createDifferenceTime > 600 && o.getQuantity() == 0) {
                    o.setQuantity(1L);
                    o.setUpdateTime(currentDate);
                    orderAuthenticationTimerMapper.updateOrderAuthenticationTimer(o);
                    RxOrder newRxOrder = new RxOrder();
                    newRxOrder.setId(oldOrder.getId());
                    newRxOrder.setEmergentStatus(2L);
                    rxOrderMapper.updateRxOrder(newRxOrder);
                    rxOrderFollow.setTitle("【发货提醒】");
                    rxOrderFollow.setContent("订单已实名后超过10分钟且未发货,请检查中台情况后尽快处理发货,若有问题请回退实名,否则系统将在20分钟后自动发货.");
                    rxOrderFollowMapper.insertRxOrderFollow(rxOrderFollow);
                } else if (createDifferenceTime > 1200 && o.getQuantity() == 1) {
                    log.info("订单_抖店状态不等于10,且已运行了超20分钟,修改这条记录");
                    o.setQuantity(2L);
                    o.setUpdateTime(currentDate);
                    orderAuthenticationTimerMapper.updateOrderAuthenticationTimer(o);
                    RxOrder newRxOrder = new RxOrder();
                    newRxOrder.setId(oldOrder.getId());
                    newRxOrder.setEmergentStatus(2L);
                    rxOrderMapper.updateRxOrder(newRxOrder);
                    rxOrderFollow.setTitle("【发货提醒】");
                    rxOrderFollow.setContent("订单已实名后超过20分钟且未发货,请检查中台情况后尽快处理发货,若有问题请回退实名,否则系统将在10分钟后自动发货.");
                    rxOrderFollowMapper.insertRxOrderFollow(rxOrderFollow);
                } else if (createDifferenceTime > 1800 && o.getQuantity() == 2) {
                    orderAuthenticationTimerMapper.deleteOrderAuthenticationTimerById(o.getId());
                    AccessToken token = DouDianToken.getToken(oldOrder.getDoudianOrderShopId());
                    DouDianAPI.review("200", oldOrder.getDoudianOrderId(), token);
                    Thread.sleep(2000L);
                    OrderLogisticsAddResponse orderLogisticsAddResponse = DouDianAPI.logisticsAdd(oldOrder.getDoudianOrderId(), token);
                    if ("10000".equals(orderLogisticsAddResponse.getCode())) {
                        rxOrderFollow.setTitle("【触发自动发货】");
                        rxOrderFollow.setContent("订单已实名后超过30分钟且未发货,系统已触发自动发货,请知晓.");
                    }
                    rxOrderFollowMapper.insertRxOrderFollow(rxOrderFollow);
                    RxOrder newRxOrder = new RxOrder();
                    newRxOrder.setId(oldOrder.getId());
                    newRxOrder.setEmergentStatus(1L);
                    rxOrderMapper.updateRxOrder(newRxOrder);
                }
            } else {
                orderAuthenticationTimerMapper.deleteOrderAuthenticationTimerById(o.getId());
            }
        }
    }
}
